<template>
  <div>
    <el-card class="box-card-header" style="padding-top: 10px;">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="产品名字">
          <el-input v-model="formInline.user" placeholder="请输入产品名字" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="searchDo(formInline.user)">查询</el-button>
          <el-button type="primary" @click="clearable()">清空查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 表格卡片-->
   <el-card class="box-card-content" v-bind:style="{ minHeight: Height + 'px' }" >

      <div id="main" style="width: 750px;height: 400px" v-if="isShow"></div>
      <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange" v-if="isShow1">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column prop="productno" label="产品编号" width="120">
        </el-table-column>
        <el-table-column prop="productname" label="产品名字" width="300">
        </el-table-column>
        <el-table-column prop="productprice" label="单价" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="productvolume" label="销量" show-overflow-tooltip>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="200">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click.native.prevent="deleteRow(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-card shadow="never" v-if="isShow1">
      <el-pagination style="margin-top: 20px;" @current-change="handleCurrentChange" :current-page.sync="currentPage1"
        :page-size="pageSize" background='true' layout="total, prev, pager, next" :total="totleSize">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        pageSize: 5,
        currentPage1: 1,
        totleSize: 0,
        Height: 0,
        Data:"",
        tableData: [],
        multipleSelection: [],
        isShow: false,
        isShow1:true,
        formInline: {
          user: '',
          region: ''
        }
      };
    },
    created(){
      this.myEcharts();
      this.getdata();
    },
    methods: {
      getdata(){
        this.getRequest('Sales/achievement/getStateList?beginPage=' + this.currentPage1 + '&pageSize=' + this.pageSize)
          .then(successResponse => {
            if (successResponse.data.status == 200) {
              this.tableData = successResponse.data.obj;
              this.totleSize = successResponse.data.totleSize;
            } else
              this.$message.error(successResponse.data.msg);
          })
          .catch(failResponse => {
            this.$message.error('网络不通畅');
          })
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      handleCurrentChange(val) {
        this.getdata()
      },
      handleEdit(index, row) {
        console.log(index, row);
      },
      deleteRow(index, rows) {
        rows.splice(index, 1);
      },
      onSubmit() {
        console.log('submit!');
      },
      handleClick(tab, event) {
        console.log(tab, event);
      },
      searchDo:function () {
        this.isShow = true;
        this.isShow1 = false;
        this.myEcharts();
      },
      clearable(){
        this.isShow =false;
        this.isShow1 =true;
        this.formInline.user = "";
      },


      myEcharts() {
        // 基于准备好的dom，初始化echarts实例
        var myChart = this.$echarts.init(document.getElementById('main'));
        // 指定图表的配置项和数据
        var option = {
          title: {
            text: '产品业绩查询: '+this.formInline.user
          },
          tooltip: {},
          legend: {
           data: ['销量']
          },
          xAxis: {
            data: []
          },
          yAxis: {},
          series: [{
           /* name: '销量', */
            type: 'bar',
            data: []
          }]
        };
        // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option);

        //AJAX获取后台数据
        this.$axios
          .get('http://localhost:777/api/Sales/achievement/getAchievement?name='+this.formInline.user)
          .then(response => {
             myChart.setOption({
                  xAxis: {
                      data: response.data.obj.x
                  },
                  series: [{
                      name: '季度销量',
                      data: response.data.obj.y
                  }]
             });
          })
          .catch(function (error) { // 请求失败处理
            console.log(error);
          });
      }
    },
    mounted() {
      //动态设置内容高度 让footer始终居底   header+footer的高度是100
      this.Height = document.documentElement.clientHeight - 375; //监听浏览器窗口变化
      window.onresize = () => {
        this.Height = document.documentElement.clientHeight - 375;
      };
        this.myEcharts();
    },
  };
</script>

<style>
  .box-card-header {
    width: 100%;
  }

  .box-card-content {
    width: 100%;
  }
</style>
